package com.asj.house.controller;

import com.asj.house.client.HouseUserClient;
import com.asj.house.constant.RedisConstants;
import com.asj.house.domain.HouseUser;
import com.asj.house.enums.SessionEnum;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.returns.RespTable;
import com.asj.house.utils.RedisUtil;
import com.asj.house.vo.param.LoginParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户管理 前端控制器
 * </p>
 *
 * @author asj
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/house-user")
@Slf4j
@Api(value = "用户相关接口", tags = "用户相关接口")
public class HouseUserController {

    @Resource
    private HouseUserClient houseUserClient;

    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/loginUser")
    @ApiOperation(value = "管理员登陆")
    public RespEntity loginUser(@RequestParam("username") String username,
                                @RequestParam("password") String password,
                                @RequestParam("mobile") String mobile,
                                @RequestParam("code") String code,
                                HttpSession session){

        String oldCode = redisUtil.get(RedisConstants.key.getSMS(mobile));
        if (! StringUtils.equals(code, oldCode)) {
            return new RespEntity(RespCode.CODE_ERROR);
        }
        RespEntity<HouseUser> respEntity = houseUserClient.login(new LoginParam(username, password, mobile));
        if (Objects.isNull(respEntity.getData())) {
            return new RespEntity(RespCode.LOGIN_ERROR, respEntity.getMessage());
        }
        HouseUser houseUser = (HouseUser) respEntity.getData();
        session.setAttribute(SessionEnum.USER.getCode(), houseUser);
        return new RespEntity(RespCode.SUCCESS);
    }

    @PostMapping("/welcome")
    @ApiOperation(value = "首页欢迎语和头像展示")
    public RespEntity<HouseUser> welcome(HttpSession session) {
        HouseUser user = (HouseUser) session.getAttribute(SessionEnum.USER.getCode());
        return new RespEntity(RespCode.SUCCESS, user);
    }

    @PostMapping("/exitUser")
    @ApiOperation(value = "退出登陆功能")
    public RespEntity exitUser(HttpSession session) {
        session.removeAttribute(SessionEnum.USER.getCode());
        return new RespEntity(RespCode.SUCCESS);
    }

    @GetMapping("/list")
    @ApiOperation(value = "用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "limit", value = "每页数量", required = true)
    })
    public RespTable<List<HouseUser>> queryAllUser(@RequestParam(value = "page", required = true) Integer page,
                                                   @RequestParam(value = "limit", required = true) Integer limit) {
        RespTable<List<HouseUser>> respTable = houseUserClient.queryAllUser(page, limit);
        return respTable;
    }

    @GetMapping("/search")
    @ApiOperation(value = "搜索用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "limit", value = "每页数量", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "column", value = "字段名", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "limit", value = "字段值", required = true)
    })
    public RespTable<List<HouseUser>> searchUser(@RequestParam(value = "page", required = true) Integer page,
                                                 @RequestParam(value = "limit", required = true) Integer limit,
                                                 @RequestParam(value = "column", required = true) String column,
                                                 @RequestParam(value = "value", required = true) String value) {
        RespTable<List<HouseUser>> respTable = houseUserClient.searchUser(page, limit, column, value);
        return respTable;
    }

    @GetMapping("/send")
    @ApiOperation(value = "发送短信验证码")
    @ApiImplicitParam(paramType = "query", dataType = "String", name = "mobile", value = "手机号", required = true)
    public RespEntity<Integer> sendMsg(@RequestParam(value = "mobile", required = true) String mobile) {
        System.out.println(mobile);
        RespEntity<Integer> respEntity = houseUserClient.sendSms(mobile);
        return respEntity;
    }

}
